/*
 * @Author: your name
 * @Date: 2022-01-31 15:18:29
 * @LastEditTime: 2022-02-01 21:22:53
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue3-ts-food-admin\src\utils\request.ts
 */
import axios, { AxiosResponse } from "axios";
import { IResponse } from "@/types/index";
// import message from 'message';
import { getToken } from "./tool";
// import getConfig from '@/config';

const request = axios.create({
  baseURL: "/api",
  timeout: 50000,
});

request.interceptors.request.use(
  (c) => {
    const token = getToken();
    if (token) {
      // eslint-disable-next-line
      c.headers.Authorization = `Bearer ${token}`;
    }
    return c;
  },
  (error) => {
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  (response: AxiosResponse<IResponse>) => {
    const { status, data } = response;
    if (status === 200 && data.code === 200) {
      return response;
    }
    return Promise.reject(data.error);
  },
  (error) => {
    const { response } = error;
    if (response) {
      const { data } = response;
      // message.error(data.error.message);
      return Promise.reject(data.error);
    }
    // message.error('服务器错误');
    return Promise.reject(error);
  }
);

export default request;
